var home = angular.module('home',['ui.router']);
/*define var*/
var config = {
	moduleName : home.name,
	tpl_folder : 'template',
}
var tpl_path = [rootConfig.moduleDir,config.moduleName,config.tpl_folder].join('/');
var baseState = [config.moduleName].join('');
var baseController = [config.moduleName].join('');
var listState = [baseState,'list'].join('.');
var listController = [baseState,'list'].join('.');
var loginState = ['login'].join('');
var loginController = [config.moduleName,'login'].join('.');
var api = {
	user : 'users/users/user',
	login: 'users/users/login',
	logout :'users/users/logout'
}
home.config(function($stateProvider, $urlRouterProvider){
	$stateProvider
	    .state(baseState, {
	      url: '/home',
	      templateUrl: [tpl_path,'main.html'].join('/'),
	      controller : baseController
	    })
	    .state(listState,{
	    	url : '/list',
	    	templateUrl : [tpl_path,'list.html'].join('/'),
	    	//controller : listController
	    })
	    .state(loginState,{
	    	url : '/login',
	    	templateUrl : [tpl_path,'login.html'].join('/'),
	    	controller: loginController,
	    })
});

home.controller(baseController, ['$scope','$http','$state','$rootScope', function ($scope,$http,$state,$rootScope) {
	$scope.logout = function()
	{
  		var url = [rootConfig.baseUrl,api.logout].join('/');
  		$http.get(url).success(function(data){
  			$state.go(loginState);
  		});
  	}
}])
home.controller(loginController,['$scope','rootService','homeService','$state','$http',function($scope,rootService,homeService,$state,$http){
	$scope.formData = {};
	$scope.signup = function(formData){
		var data = {
			email : formData.email,
			username : formData.username,
			password : formData.password
		};
		flag = rootService.validateForm(data);
		if(flag){
			homeService.Users.add(data).success(function(res){
				if(res.status==false){
					box.alert('Thông báo','Có lỗi trong quá trình đăng kí!','red');
				}else{
					$state.go(loginState);
				}
			});		
		}
	}
	$scope.login = function(formData){
		var data = {
			username : formData.username,
			password : formData.password,
			save 	 : formData.save ? 'y' : 'n'
		}
		flag = rootService.validateForm(data);
		if(flag){
			rootConfig.loading(true,'#loginForm');
			homeService.Users.login(data).success(function(res){
				if(res.status==false){
					box.alert('Thông báo','Thông tin đăng nhập chưa chính xác!','red');
				}else{
					$state.go(baseState);
				}
			});
		}
		rootConfig.loading(false);
	}
	$scope.logout = function(){
		//rootConfig.loading(true,'body');
		var url = [rootConfig.baseUrl,api.logout].join('/');
		$http.get(url).success(function(data){
			console.log(data);
		});
	}
}]);
home.controller(listController, ['$scope', function ($scope) {
	
}])
home.factory('homeService', ['$http',function ($http) {
	return {
		Users : {
			add : function(data){
				var url = [rootConfig.baseUrl,api.user].join('/');
				return $http.post(url,data);
			},
			login : function(data){
				var url = [rootConfig.baseUrl,api.login].join('/');
				return $http.post(url,data);
			},
			logout : function(){
				var url = [rootConfig.baseUrl,api.logout].join('/');
				return $http.get(url);
			}
		}
	}
}])